<!--
  -    Copyright (c) 2018-2025, gewu All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the gewucn.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: gewu
  -->
<template>
  <div class="execution">
    <el-card class="box-card btn-search page-search">
      <div slot="header" class="clearfix">
        <div class="btn-box">
          <el-button
            type="info"
            icon="el-icon-refresh-left"
            @click="refreshChange()"
          ></el-button>
          <el-button
            id="gwButton"
            type="primary"
            style="backgroundcolor: #e1b980"
            icon="el-icon-circle-plus-outline"
            v-if="permissions.ems_emsinsinspectplan_add"
            @click="$refs.crud.rowAdd()"
            >新增</el-button
          >
          <el-button
            type="success"
            icon="el-icon-edit"
            v-if="permissions.ems_emsinsinspectplan_edit"
            :disabled="single"
            @click="handleEdit"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-circle-close"
            v-if="permissions.ems_emsinsinspectplan_del"
            @click.native="handleDel()"
            :disabled="multiple"
            >删除</el-button
          >
          <el-button type="check" icon="el-icon-download" @click="exportExcel"
            >导出</el-button
          >
        </div>
        <div class="icon-box">
          <i class="el-icon-search" @click="searchShow"></i>
          <i class="el-icon-refresh" @click="refreshChange"></i>
          <i class="el-icon-goods"></i>
          <i class="el-icon-setting" @click="columnShow"></i>
          <i class="icon-zuixiaohua" />
        </div>
      </div>
    </el-card>
    <basic-container>
      <avue-crud
        ref="crud"
        v-model="form"
        :page.sync="page"
        :cell-style="cellStyle"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        :before-open="handleOpenBefore"
        @selection-change="selectionChange"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel"
        @cell-click="cellClick"
      >
        <template slot="header">
          <IconTitle class="selfTitle" title="巡检计划" imgUrl="yunwei" />
        </template>
        <template slot="deptId" slot-scope="scope">
          {{ scope.row.deptName }}
        </template>
        <template slot="inspectCycle" slot-scope="scope">
          {{ scope.row.inspectCycle }}
        </template>
        <template slot="generateTime" slot-scope="scope">
          {{ scope.row.generateTime }}
        </template>
        <template slot="noticeTime" slot-scope="scope">
          {{ scope.row.noticeTime }}
        </template>
        <template slot="planEffectiveTime" slot-scope="scope">
          {{ scope.row.planEffectiveTime }}
        </template>
        <template slot="taskEffectiveTime" slot-scope="scope">
          {{ scope.row.taskEffectiveTime }}
        </template>
        <template slot="liableUserId" slot-scope="scope">
          {{ scope.row.liableUserName }}
        </template>
        <template slot="reviewerId" slot-scope="scope">
          {{ scope.row.reviewer }}
        </template>
        <!-- <template slot="status" slot-scope="scope">
          {{ scope.row.status }}
        </template> -->
        <template slot="liableUserIdForm" slot-scope="scope">
          <el-input
            v-model="form.liableUserName"
            @focus="selectUserDia('liableUserName','liableUserId')" 
            placeholder="请选择负责人"
          />
        </template>
        <template slot="reviewerIdForm" slot-scope="scope">
          <el-input
            v-model="form.reviewer"
            @focus="selectUserDia('reviewer','reviewerId')"
            placeholder="请选择审核人"
          />
        </template>
         <template slot="deptIdForm" slot-scope="scope">
          <avue-input-tree
            v-model="form.deptId"
            :dic="treeDeptData"
            :props="defaultProp"
            placeholder="请选择所属部门"
          />
        </template>
        <template slot-scope="scope" slot="menu"> 
          
           <el-button type="text" v-if="scope.row.enable==0" @click="handleEdit1(scope.row,scope.index)">
            <i class="el-icon-edit"></i>修改</el-button
          >
           <el-button type="text" v-if="scope.row.enable==0" @click="handleDel(scope.row)">
            <i class="el-icon-delete"></i>删除</el-button
          >
          <el-button type="text" @click="goLook(scope.row)">
            <i class="el-icon-view"></i>查看</el-button
          >
           <el-button type="text" v-if="scope.row.enable==0" @click="submitPlan(scope.row)">
            <i class="el-icon-s-check"></i>提交申请</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
    <el-drawer
      class="drawerStyle"
      title="计划信息"
      :show-close="false"
      :visible.sync="detail"
      direction="rtl"
      size="50%"
      append-to-body
       :modal="false"
    >
      <div>
        <div>
          <span class="labelS">计划名称：</span>
          <span class="contentS">{{detailInfo.planName}}</span>
        </div>
        <div>
          <span class="labelS">计划编号：</span>
          <span class="contentS">{{detailInfo.planNum}}</span>
        </div>
        <div>
          <span class="labelS">负责人：</span>
          <span class="contentS">{{detailInfo.createBy}}</span>
        </div>
      </div>
      <div class="statusList">
        <span class="labelS">描述信息</span>
        <span class="detailInfo">任务状态</span>
        <span class="dottedStyle"></span>
        <span class="statusContent">{{detailInfo.enable=='0'?'不启用':'启用'}}</span>
      </div>
      <div class="line"></div>
      <div>
        <div class="icon-style">
          <div class="con-right">
            <div class="my-steps-box">
              <template v-for="(item, index) in menulist">
                <div
                  :class="[
                    'step-item',
                    activeMenu == index ? 'step-active' : '',
                  ]"
                  :key="index"
                >
                  <div class="top-title">
                    <span class="text">{{ item.label }}</span>
                  </div>
                </div>
              </template>
            </div>
          </div>
          <div class="con-left" id="scrollBox">
            <el-card shadow="always" class="box-card">
              <div class="tableTitle">计划详情</div>
              <div>
                <span class="labelS">计划编号：</span>
                <span class="contentS">{{detailInfo.planNum}}</span>
              </div>
              <div>
                <span class="labelS">计划名称：</span>
                <span class="contentS">{{detailInfo.planName}}</span>
              </div>
              <div>
                <span class="labelS">负责人：</span>
                <span class="contentS">{{detailInfo.liableUser}}</span>
              </div>
              <!-- <div>
                <span class="labelS">计划开始时间：</span>
                <span class="contentS">{{detailInfo.liableUser}}</span>
              </div> -->
              <div>
                <span class="labelS">巡检周期：</span>
                <span class="contentS">{{detailInfo.inspectCycle}}</span>
              </div>
              <div>
                <span class="labelS">创建人：</span>
                <span class="contentS">{{detailInfo.createBy}}</span>
              </div>
              <div>
                <span class="labelS">创建时间：</span>
                <span class="contentS">{{detailInfo.createTime}}</span>
              </div>
            </el-card>
            <el-card shadow="always" class="box-card">
              <div class="tableTitle">最新任务</div>
              <div>
                <span class="labelS">任务状态：</span>
                <span class="contentS">待执行</span>
              </div>
              <div>
                <span class="labelS">执行时间：</span>
                <span class="contentS">无</span>
              </div>
              <div>
                <span class="labelS">异常项个数：</span>
                <span class="contentS">0</span>
              </div>
              <div>
                <span class="labelS">漏检项个数：</span>
                <span class="contentS">0</span>
              </div>
            </el-card>
          </div>
        </div>
      </div>
    </el-drawer>
    <SelectliableUser
      ref="selectLiable"
      v-if="selectLiableDia"
      :changeidentityDia.sync="selectLiableDia"
      @closeSelect='selectUser'
    ></SelectliableUser>
    
  </div>
</template>
<script>
import {
  fetchList,
  addObj,
  putObj,
  delObj,
  fetchListed,
  submitPlan
} from "@/api/ems/inspection/plan";

import { fetchTree } from "@/api/admin/dept";
import { tableOption } from "@/const/crud/ems/inspection/plan";
import { mapGetters } from "vuex";
import jQuery from "jquery";
import IconTitle from "@/components/icon-title/index.vue";
import SelectliableUser from "@/views/ems/equipment/components/selectliableUser.vue";
export default {
  name: "plan",
  components: {
    IconTitle,SelectliableUser
  },
  data() {
    return {
      tableData: [],
      treeDeptData:[],
      dialogVisible: false,
      detailInfo:{},
      list: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      defaultProp:{
label: "name",
        value: "id",
      },
      form: {},
      formName:{},
       newName:'',
                 newId:'',
      treeData: [],
      searchForm: {}, // 查询参数
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      pageA: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
      },
      tableLoading: false,
      tableOption: tableOption,
      ids: [],
      selectionList: [],
      detail: false,
      activeMenu: 0,
      menulist: [
        {
          label: "计划详情",
        },
        {
          label: "审批信息",
        },
      ],
      selectLiableDia: false,
    };
  },
  computed: {
    ...mapGetters(["permissions", "theme"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.ems_emsinsinspectplan_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.ems_emsinsinspectplan_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.ems_emsinsinspectplan_edit,
          false
        ),
      };
    },
  },
  mounted() {
    this.initElement();
    this.changeThme();
    fetchTree().then((response) => {
      this.treeData = response.data.data;
      this.treeDeptData = response.data.data;
    });
   
  },
  methods: {
     cellStyle(data) {
      // 表格的第二列加上小手和padding为0
      if (data.columnIndex === 1) {
        return "color:#02b980;cursor:pointer";
      }
    },
    cellClick(row, column) {
      console.log(column)
      if (column.property === "planNum") {
        this.$router.push({
          path: "/ems/inspection/plan/detail",
          query: { id: row.id },
        });
      } else if(column.label === "操作"){

        }else {
        this.detailInfo =row;
         this.detail = true;
      }
    },
    nodeClick(data) {
      this.pageA.page = 1;
      this.getListUser(this.pageA, { deptId: data.id });
    },
    getListUser(page, params) {
      fetchListed(
        Object.assign(
          {
            current: this.pageA.currentPage,
            size: this.pageA.pageSize,
          },
          params
        )
      ).then((response) => {
        this.list = response.data.data.records;
        this.page.total = response.data.data.total;
      });
    },
    // selectUser(row) {
    //   this.form.liableUser = row.username;
    //   this.form.liableUserId = row.userId;
    //   this.dialogVisible = false;
    // },
     selectUserDia(m,n) {
      this.selectLiableDia = true;
      this.newName = m;
      this.newId = n;
      this.$nextTick(() => {
        this.$refs.selectLiable.open();
      });
    },
     selectUser(row) {
      // this.formName[this.newName] = row.username;
      this.form[this.newId] = row.userId;
      this.form[this.newName] = row.username;
      this.selectLiableDia = false;
    },
    initElement() {
      var mediumb = document.createElement("b"); //思路一样引入中间元素
      jQuery(".avue-crud__tip").after(mediumb);
      jQuery(".selfTitle").after(jQuery(".avue-crud__tip"));
      jQuery(mediumb).after(jQuery(".selfTitle"));
      jQuery(mediumb).remove();
    },
    selectionChange(list) {
      this.selectionList = list;
      this.single = list.length !== 1;
      this.multiple = !list.length;
      this.ids = list.map((item) => item.id);
    },

    columnShow() {
      this.$refs.crud.$refs.dialogColumn.columnBox = !0;
    },
    // 搜索框显示与否
    searchShow() {
      this.$refs.crud.$refs.headerSearch.searchShow =
        !this.$refs.crud.$refs.headerSearch.searchShow;
    },
    handleOpenBefore(show, type){
       window.boxType = type;
       if (type === "add") {
         for(let key  in this.form){
            this.form[key] = undefined
            }
      }
      show();
    },
    // 列表查询
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
          },
          params,
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    //编辑
    handleEdit() {
      var refsDate = this.$refs;
      refsDate.crud.rowEdit(
        this.selectionList[0],
        this.selectionList[0].$index
      );
    },
     handleEdit1(row, index) {
      this.$refs.crud.rowEdit(row, index)
    },
    // 删除
    handleDel: function (row, index) {
      this.$confirm("是否确认删除所选数据项", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = "";
          if (row) {
            id = row.id;
          } else {
            id = this.ids;
          }
          return delObj(id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
  //提交申请
    submitPlan: function (row, index) {
      console.log(row)
      this.$confirm("是否确认提交申请", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(()=>{
        return submitPlan(row.id);
      })
       .then(() => {
          this.$message.success("提交成功");
          this.getList(this.page);
        });
    },
    // 更新
    handleUpdate: function (row, index, done, loading) {
      putObj(row)
        .then((data) => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    // 保存
    handleSave: function (row, done, loading) {
      addObj(row)
        .then((data) => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
          
        })
        .catch(() => {
          loading();
        });
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current;
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page);
    },
    // 导出excel
    exportExcel() {
      this.downBlobFile(
        "/ems/emsinsinspectplan/export",
        this.searchForm,
        "emsinsinspectplan.xlsx"
      );
    },
    // 改变主题颜色
    changeThme() {
      //"#02b980"
      document.getElementById("gwButton").style.backgroundColor = this.theme;
    },
    goLook(row) {
      
       this.$router.push({
          path: "/ems/inspection/plan/detail",
          query: { id: row.id },
        });
      
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/color.scss";
@import "@/styles/drawer.scss";
.statusList {
  margin-top: 20px;
  .labelS {
    color: #c1c8d2;
  }
  .dottedStyle {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    margin-right: 5px;
    background-color: #f7ca60;
  }
  .detailInfo {
    color: #5d5d5d;
    font-size: 12px;
    margin-right: 10px;
  }
  .statusContent {
    font-size: 12px;
  }
}


.icon-style {
  display: flex;
}
.con-left {
  width: 80%;
  height: 400px;
  overflow-y: scroll;
  .contentS {
    font-weight: normal;
  }
  .small-title {
    font-size: 12px;
    font-weight: 600;
    margin: 10px 15px;
    display: block;
  }
}
.con-right {
  width: 20%;
  border-right: 1px solid #ecf0f4;
  padding-left: 23px;
  text-align: center;
  box-sizing: border-box;

  .my-steps-box {
    font-size: 12px;
    margin-top: 50px;
    .step-item {
      margin-bottom: 15px;
      padding: 6px 0;
      position: relative;
      .text {
        cursor: pointer;
      }
    }
    .step-item.step-active {
      border-right: 2px solid #26ae61;
      .text {
        color: $theme;
      }
    }
    .step-item:nth-last-child(1) .line {
      display: none;
    }
  }
}
</style>